package com.oocl.zero.datahandle.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.oocl.zero.datahandle.entity.Customer;



public class CustomerParser{

	// = "\\s+";
//	private String splitRegex= "\\s+";
//	public CustomerParser(String splitRegex){
//		this.splitRegex=splitRegex;
//	}
	
	public static Customer doParse(String line,String splitRegex) {
		//
		String[] ss = line.split(splitRegex);
		Map<Integer,String> dataMap=new HashMap<Integer, String>();
		for(int i=1;i<=ss.length;i++){
			dataMap.put(i, ss[i-1]);
		}
		//
		boolean isFiltered = CustomerFilter.isFiltered(dataMap);
		if(!isFiltered){
			return getCustomerInstanceBy(dataMap);
		}
		return null;
	}

	//input format:id(1) name(2) email(3) gender(4) birthday(5)
	private static Customer getCustomerInstanceBy(Map<Integer, String> map) {
		Customer c=new Customer();
		try {
			c.setId( Integer.parseInt(map.get(1)) );
			c.setName( map.get(2) );
			c.setEmail( map.get(3) );
			c.setGender( "true".equals(map.get(4)) );
			long time = new SimpleDateFormat( Customer.DATE_FORMAT ).parse( map.get(5) ).getTime();
			c.setBirthday(time);
		} catch (ParseException e) {
			return null;
		}
		return c;
	}
	
	
}
